package com.demo.jinjunhuang.viewsdemo.practice;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.demo.jinjunhuang.viewsdemo.R;

/**
 * @author jianjunhuang.me@foxmail.com
 *         create on 2017/9/2.
 */

public class ClipPathView extends View {
    public ClipPathView(Context context) {
        super(context);
    }

    public ClipPathView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ClipPathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ClipPathView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private Paint paint = new Paint();

    private Path path = new Path();

    private int left;

    private int bottom;

    private Bitmap bitmap;

    {
        paint.setAntiAlias(true);
        paint.setColor(Color.DKGRAY);
        bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.cat);
        path.setFillType(Path.FillType.INVERSE_WINDING);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    private static final String TAG = "ClipPathView";

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        path.addCircle(getWidth() / 2, getHeight() / 2, 100, Path.Direction.CW);
        canvas.save();
        canvas.clipPath(path);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        canvas.restore();
    }
}
